package me.j360.springboot.simple.bean.search;

import me.j360.springboot.simple.bean.BaseEnum;
import org.springframework.util.Assert;

/**
 * 说明：
 * Bean类 - 查询排序条件
 *
 * ============================================================================
 * 版权所有 2015 qiushihua。
 *
 * @author qiushihua
 * @version 0.1 2015-07-22
 * ============================================================================
 */
public class Order {

    private static final String DEFAULT_ORDERBY = "createDate";
    private static final BaseEnum.OrderType DEFAULT_ORDERTYPE = BaseEnum.OrderType.desc;

    private final String orderBy;
    private final BaseEnum.OrderType orderType;

    private Order(String orderBy, BaseEnum.OrderType orderType) {
        this.orderBy = orderBy;
        this.orderType = orderType;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public BaseEnum.OrderType getOrderType() {
        return orderType;
    }

    private static Builder newBuilder() {
        return new Builder();
    }

    public static Order order(String orderBy, BaseEnum.OrderType orderType) {
        return newBuilder().setOrderBy(orderBy).setOrderType(orderType).build();
    }

    public static Order simpleOrder() {
        return newBuilder().setOrderBy(DEFAULT_ORDERBY).setOrderType(DEFAULT_ORDERTYPE).build();
    }

    public static class Builder {

        private String orderBy;
        private BaseEnum.OrderType orderType;

        public Builder setOrderBy(String orderBy) {
            this.orderBy = orderBy;
            return this;
        }

        public Builder setOrderType(BaseEnum.OrderType orderType) {
            this.orderType = orderType;
            return this;
        }

        public Order build() {
            Assert.hasText(orderBy, "orderBy 不能为空");
            Assert.notNull(orderType, "orderType 不能为空");
            return new Order(orderBy, orderType);
        }

    }

}
